﻿/// <reference path="../../../knockout-2.1.0.js" />
/// <reference path="../Common.js" />

// todo: make sure you reset the VM every time you close the modal

$(document).ready(function () {
  getCategories(); // it should be called SETcategories
  ko.applyBindings(new CategoryViewModel(), $('#category')[0]);
});

function getCategories() {
  var request = $.ajax({
    url: '/Admin/Categories/GetCategories',
    data: {},
    dataType: 'json',
    type: 'GET',
    async: false
  });

  request.done(function (data) {
    // bind list
    ko.applyBindings(new CategoriesViewModel(data), $('#categories')[0]);
  });

  request.fail(function (jqXhr, textStatus) {
    whenRequestFailed(jqXhr, textStatus);
  });
}

function CategoriesViewModel(data) {
  if (!$.browser.msie) {
    // todo: fix this because it doesnt work on IE8
    $.each(data, function (index, value) {
      value.url = '/Admin/SubCategories/Index/' + value.Id;
      value.remove = function () {
        removeCategory(value.Id);
      };
      value.edit = function () {
        editCategory(value.Id);
      };
    });
  }
  this.categories = ko.observableArray(data);
}

function CategoryViewModel() {
  this.id = ko.observable(0);
  this.name = ko.observable('');
  this.add = function () {
    addCategory(this.name);
  };
}

function addCategory(name) {
  var request = $.ajax({
    url: '/Admin/Categories/AddCategory',
    type: 'GET',
    data: { name: name },
    dataType: 'json',
    async: false
  });

  request.done(function (data) {
    // close modal
    $('#category').modal('hide');

    resetViewModelAndReApllyBindings();

    // get categories
    getCategories();
    // show success message
    showSuccessBoxMessage('inserted');
  });

  request.fail(function (jqXhr, textStatus) {
    whenRequestFailed(jqXhr, textStatus);
  });
}

function removeCategory(id) {
  var request = $.ajax({
    url: '/Admin/Categories/RemoveCategory',
    type: 'GET',
    data: { id: id },
    dataType: 'json',
    async: false
  });

  request.done(function (data) {
    // get categories
    getCategories();
    // show success message
    showSuccessBoxMessage('removed');
  });

  request.fail(function (jqXhr, textStatus) {
    whenRequestFailed(jqXhr, textStatus);
  });
}

function editCategory(id) {
  // get category
  var request = $.ajax({
    url: '/Admin/Categories/GetCategory',
    type: 'GET',
    data: { id: id },
    dataType: 'json',
    async: false
  });

  request.done(function (data) {
    // todo
    //debugger;
    var viewModel = new CategoryViewModel();
    viewModel.id = data.Id;
    viewModel.name = data.Name;
    // overriding the add method (I don't like it)
    viewModel.add = function () {
      updateCategory(viewModel);
    };
    ko.cleanNode($('#category')[0]);
    ko.applyBindings(viewModel, $('#category')[0]);
    // open modal
    $('#category').modal('show');
  });

  request.fail(function (jqXhr, textStatus) {
    whenRequestFailed(jqXhr, textStatus);
  });
}

function showSuccessBoxMessage(verb) {
  $('#verb').html(verb);
  $('#itemBoxMessage').fadeIn(500, function () {
    $('#itemBoxMessage').fadeOut(5000);
  });
}

function updateCategory(viewModel) {
  // get category
  var request = $.ajax({
    url: '/Admin/Categories/UpdateCategory',
    type: 'GET',
    data: { id: viewModel.id, name: viewModel.name },
    dataType: 'json',
    async: false
  });

  request.done(function (data) {
    // close modal
    $('#category').modal('hide');

    resetViewModelAndReApllyBindings();

    // get categories
    getCategories();
    // show success message
    showSuccessBoxMessage('updated');
  });

  request.fail(function (jqXhr, textStatus) {
    whenRequestFailed(jqXhr, textStatus);
  });
}

function resetViewModelAndReApllyBindings() {
  ko.cleanNode($('#category')[0]);
  ko.applyBindings(new CategoryViewModel(), $('#category')[0]);
}
